<?php
/**
 * Created by PhpStorm.
 * User: Qarluq
 * Date: 15/6/29
 * Time: 下午4:27
 */
class ContentAction extends CommonAction{
    /**内容列表*/
    public function index(){
        import('ORG.Util.Page');
        $db=D('ContentView');
        $cat=M('category')->where(array('en_name'=>array('eq',I('cat_name'))))->find();
        $this->cat_name=$cat['name'];
        $this->cat_en_name=$cat['en_name'];
        if (I('cat_name')) {
            $count=$db->where(array('con_category'=>array('eq',$cat['en_name'])))->order('add_time ASC')->count();
            $page=new page($count,10);
            $limit=$page->firstRow.','.$page->listRows;
            $message =$db->where(array('con_category'=>array('eq',I('cat_name'))))->order('add_time ASC')->limit($limit)->select();
        } elseif (I('search_obg')) {
            $search_obg['con_title|con_content']  = array('like', '%'.$_POST['search_obg'].'%');
            $count=$db->where($search_obg)->order('add_time ASC')->count();
            $page=new page($count,10);
            $limit=$page->firstRow.','.$page->listRows;
            $this->obg=I('search_obg');
            $message =$db->where($search_obg)->order('add_time ASC')->limit($limit)->select();
        } else {
            $count=$db->order('add_time ASC')->count();
            $page=new page($count,10);
            $limit=$page->firstRow.','.$page->listRows;
            $message =$db->order('add_time ASC')->limit($limit)->select();
        }
        $this->page=$page->show();
        $this->con=$message;
        $this->display();
    }
    /*导入编辑模板*/
    public function edit(){
        $con=M('content')->where(array('id'=>array('eq',I('id'))))->find();
        $cat=M('category')->where(array('en_name'=>array('eq',$con['con_category'])))->find();
        $this->category_cn_name=$cat['name'];
        include('show.php');
        $this->display();
    }
    /*删除内容*/
    public function delete(){
        $con=M('content')->where(array('id'=>array('eq',I('id'))))->find();
        $pic_url= APP_NAME. '/uploads/thomb/'.$con['con_pic'];
        $con_file=$con['con_file'];
        $file_url=explode(',',$con['con_file']);
        $con_video=$con['con_video'];
        $video_url=explode(',',$con['con_video']);
        if (file_exists($pic_url)) {
            delete_file($pic_url);
        }

        if (!empty($con_file)) {
            foreach($file_url as $v){
                delete_file($v);
            }
        }

        if (!empty($con_video)) {
            foreach($video_url as $v){
                delete_file($v);
            }
        }

        if(M('content')->where(array('id'=>array('eq',I('id'))))->delete()){
            $this->success('删除成功');
        }else{
            $this->error('遇到错误');
        }

    }
    /* 删除文件用Ajax* */
    public function del_vedio(){
        if(unlink($_POST['path'])){
            echo json_encode(1) ;
        }else{
        }
    }
    /*审核内容*/
    public function on_status()
    {
        if($_GET['category']=='内容'){
            $cat='';
        }
        else{
            $cat=array('cat_name'=>$_GET['category']);
        }
        if (M('content')->where(array('id' => array('eq', $_GET['rid'])))->save(array('accept' => 1))) {
            $this->success('审核成功', U('index',$cat));
        } else {
            $this->error('审核失败');
        }
    }
    /** 导入添加模板与关于信息*/
    public  function addcontent(){
        $cat=M('category')->where(array('en_name'=>array('eq',I('cat_name'))))->find();
        $this->cat_name=$cat['name'];
        $this->cat_en_name=$cat['en_name'];
        $this->display();
    }
    /* 上传视频函数  */
    public function upload_video()
    {
        import('ORG.Net.UploadFile');
        $upload_video = new UploadFile();
        $upload_video->maxSize = 20;
        $upload_video->allowExts = array('mp4'); // 设置附件上传类型
        $upload_video->savePath = 'gulbagh/uploads/video/' . date("Y-m-d") . '/';
        $upload_video->saveRule = "uniqid";
        $this->_mkdir($upload_video->savePath);
        if ($upload_video->upload()) {
            $video_info = $upload_video->getUploadFileInfo();
            $video_info = $video_info[0];
            //$video_url  =$video_info['savename'];
            $video_url  =$video_info['savepath'] . $video_info['savename'];
            $this->ajaxReturn($video_url, $video_info, 1, 'json');
        } else {
            $this->ajaxReturn('', $upload_video->getErrorMsg(), 0, 'json');
        }
    }
    /*上传文件函数 */
    public function upload_file()
    {
        import('ORG.Net.UploadFile');
        $upload_video = new UploadFile();
        $upload_video->maxSize = 20;
        $upload_video->allowExts = array( 'pdf', 'zip','rar','word','xlsx','xlsx','xlsm','xls'); // 设置附件上传类型
        $upload_video->savePath = 'gulbagh/uploads/file/' . date("Y-m-d") . '/';
        $upload_video->saveRule = "uniqid";
        $this->_mkdir($upload_video->savePath);
        if ($upload_video->upload()) {
            $video_info = $upload_video->getUploadFileInfo();
            $video_info = $video_info[0];
            $video_url  =$video_info['savepath'] . $video_info['savename'];
            $this->ajaxReturn($video_url, $video_info, 1, 'json');
        } else {
            $this->ajaxReturn('', $upload_video->getErrorMsg(), 0, 'json');
        }
    }
    /* 添加内容处理  */
    public function run_addcontent()
    {

        $data=array(
            'con_title'=>$_POST['con_title'],
            'con_category'=>$_POST['con_category'],
            'con_content'=>$_POST['content'],
            'con_video'=>substr($_POST['con_video'],0,-1),
            'add_time'=>date('Y-m-d',time()),
            'adder_name'=>session('username'),
            'adder_ip'=>get_client_ip(),
            'con_file'=>substr($_POST['con_file'],0,-1),
            'file_old_name'=>substr($_POST['file_old_name'],0,-1),
            'video_old_name'=>substr($_POST['video_old_name'],0,-1),
            'con_file_name'=>$_POST['file_name'],
            'security'=>$_POST['security'],
        );
        if (!empty($_FILES['con_thomb_pic']['name'])) {
            //如果有文件上传 上传附件
            $this->uploadify();
            $data=array_merge(array('con_pic'=>$_POST['con_pic']),$data);
        }
        if(isset($_POST['id'])){
            $data=array_merge(array('id'=>I('id')),$data);
            $run='save';
            }else{
            $run='add';
           }
        if(M('content')->$run($data)){
            $this->success('添加成功',U('index',array('cat_name'=>$_POST['con_category'])));
        }else{
            $this->error('添加失败');
        }
    }
    /**建立文件夹* */
    public function _mkdir($Folder)
    {
        if (!is_dir($Folder)) {
            $dir = explode('/', $Folder);
            foreach ($dir as $v) {
                if ($v) {
                    $d .= $v . '/';
                    if (!is_dir($d)) {
                        $state = mkdir($d);
                        if (!$state) {
                            die('ھۆججەت قۇرۇشتا' . $d . 'خاتالىق كۆرۈلدى！');
                        }
                    }
                }
            }
        }
    }
    /*上传封面文件*/
    public function uploadify()
    {
            import("ORG.Util.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize = 20*1024;
            $upload->allowExts = array('jpg','jpeg','gif','png');
            $upload->savePath = './gulbagh/uploads/thomb/';
            $upload->thumb = true; //设置缩略图
            $upload->imageClassPath = "ORG.Util.Image";
            $upload->thumbPrefix = "th"; //生成多张缩略图
            $upload->thumbMaxWidth = "330";
            $upload->thumbMaxHeight = "230";
            $upload->saveRule = uniqid; //上传规则
            $upload->thumbRemoveOrigin = true; //删除原图
            if(!$upload->upload()){
                $this->error($upload->getErrorMsg());//获取失败信息
            } else {
                $info = $upload->getUploadFileInfo();//获取成功信息
            $_POST['con_pic'] ='th'.$info[0]['savename'];

        }
    }

}
